//
//  AXFMainTabController.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/22.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit

class AXFMainTabController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self .addChildViewControllers()
    }
    //添加子控制器
    private func addChildViewControllers(){
        addChildViewControllers(Vc: AXFHomeController(), title: "首页 ", imageName: "v2_home",index:0)
        addChildViewControllers(Vc: AXFSuperMarketController(), title: "闪电超市", imageName: "v2_order",index:1)
        addChildViewControllers(Vc: AXFShoppingController(), title: "购物车", imageName: "shopCart",index:2)
        addChildViewControllers(Vc: AXFMineController(), title: "我的 ", imageName: "v2_my",index:3)
    }
    private func addChildViewControllers(Vc:UIViewController,title:String,imageName:String,index:Int){
        Vc.title = title
        //设置默认图片和选中图片
        Vc.tabBarItem.tag = index
        //修改了 tabbar 的图片大小
        let normolImage = UIImage.createNewImage(withColor: UIImage(named: imageName), multiple: 0.9)
        Vc.tabBarItem.image = normolImage?
            .withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
         //修改了 tabbar 的选中图片大小
         let selectedImage = UIImage.createNewImage(withColor: UIImage(named: imageName + "_r"), multiple: 0.9)
        Vc.tabBarItem.selectedImage = selectedImage?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置默认文字和选中文字
        Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.darkGray], for: .normal)
        Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.darkGray], for: .selected)
        //设置文字大小
        Vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 10)], for: .normal)
        //----------------- ios10.0--------------------------------------------------------------
//        //设置tabbar文字的位置
        Vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 10, vertical: 0)
      //设置导航控制器
        let Nav = UINavigationController(rootViewController: Vc)
        //添加到子控制器
        self.addChildViewController(Nav)
    }
    //获取被点击的tabbarItem的图片,对它进行形变
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //print(item.tag) //测试点击的tabBarItem是否是对应的tag
        
        //定义变量 作为下标
        var index = 0
        for subview in tabBar.subviews{
            //判断subview是否是UITabBarButton的子类或者父类
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag{
                    //说明获取到了点击的TabBarButton
                    //再遍历这个subview,这里面存的一定是tabBarButton
                    for v in subview.subviews{
                        if v.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            //print("\(v)") //v就是我们要找的图片
                            
                            //先对它进行缩小
                            v.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
                            //再让它进行还原
                            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 5, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                            }, completion: { (_) in
                                //print("完成")
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }
}


